package com.easyedu.dean.controller;

import com.easyedu.core.entity.po.OpenCourse;
import com.easyedu.core.util.R;
import com.easyedu.dean.entity.dto.request.ReqCreateOpenCourse;
import com.easyedu.dean.entity.dto.request.ReqQueryHasArrangeCourse;
import com.easyedu.dean.entity.dto.response.RespOpenCourseRange;
import com.easyedu.dean.entity.dto.response.RespQueryHasArrangeCourse;
import com.easyedu.dean.entity.dto.response.RespTimeTableIndex;
import com.easyedu.dean.service.OpenCourseService;
import java.util.List;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author lmr
 **/
@Validated
@RestController
@RequestMapping("/dean")
public class OpenCourseController {

     private final OpenCourseService openCourseService;

     public OpenCourseController(OpenCourseService openCourseService) {

          this.openCourseService = openCourseService;
     }

     /**
      * 功能描述: 查询开放课程
      *
      * @param openYear         1 开放年份
      * @param semester         2 开放学期
      * @param openInstituteId  3 开放学院id
      * @param openDepartmentId 4 开放专业id
      * @param courseCode       5 课程代码
      * @param courseNumber     6 课程序号
      * @param courseName       7 课程名称
      * @param teacherName      8  教师名称
      * @return : com.easyedu.api.util.R
      **/
     @GetMapping("/openCourse")
     public R getOpenCourse(@RequestParam(defaultValue = "0") long openYear,
             @RequestParam(required = false) String semester, @RequestParam(defaultValue = "0") long openInstituteId,
             @RequestParam(defaultValue = "0") long openDepartmentId, @RequestParam(required = false) String courseCode,
             @RequestParam(required = false) String courseNumber, @RequestParam(required = false) String courseName,
             @RequestParam(required = false) String teacherName) {

          OpenCourse openCourse = new OpenCourse();
          openCourse.setOpenYear(openYear);
          openCourse.setSemester(semester);
          openCourse.setOpenInstituteId(openInstituteId);
          openCourse.setOpenDepartmentId(openDepartmentId);
          openCourse.setCourseCode(courseCode);
          openCourse.setCourseNumber(courseNumber);
          openCourse.setCourseName(courseName);
          openCourse.setTeacherName(teacherName);
          List<OpenCourse> openCourses = openCourseService.getOpenCourse(openCourse);
          return R.ok().data(openCourses);
     }

     /**
      * 功能描述: 为课程分配教师
      *
      * @param reqCreateOpenCourse 1
      * @return : com.easyedu.api.util.R
      **/
     @PostMapping("/openCourse")
     public R createOpenCourse(@RequestBody ReqCreateOpenCourse reqCreateOpenCourse) {

          openCourseService.createOpenCourse(reqCreateOpenCourse);
          return R.ok().message("创建课程成功");
     }

     /**
      * 功能描述: 取消开课
      *
      * @param openCourseId 1
      * @return : com.easyedu.api.util.R
      **/
     @DeleteMapping("/openCourse")
     public R deleteOpenCourse(@RequestParam Long openCourseId) {

          openCourseService.deleteOpenCourse(openCourseId);
          return R.ok().message("删除课程成功");
     }

     /**
      * 功能描述:设置课程开放
      *
      * @param ids 1
      * @return : com.easyedu.api.util.R
      **/
     @PostMapping("/open/openCourse")
     public R setCourseOpen(@RequestBody List<Long> ids) {

          String res = openCourseService.openOpenCourse(ids);
          return R.ok().message(res);
     }

     /**
      * 功能描述: 关闭课程开放
      *
      * @param ids 1
      * @return : com.easyedu.api.util.R
      **/
     @PostMapping("/close/openCourse")
     public R setCourseClose(@RequestBody List<Long> ids) {

          String res = openCourseService.closeOpenCourse(ids);
          return R.ok().message(res);
     }

     /**
      * 功能描述:开启选课系统
      *
      * @return : com.easyedu.api.util.R
      **/
     @GetMapping("/selection/course/open")
     public R setSelectionCourseSystemOpen() {

          openCourseService.openSelectionCourseSystem();
          return R.ok().message("开放选课系统成功！");
     }

     /**
      * 功能描述:关闭选课系统
      *
      * @return : com.easyedu.api.util.R
      **/
     @GetMapping("/selection/course/close")
     public R setSelectionCourseSystemClose() {

          openCourseService.closeSelectionCourseSystem();
          return R.ok().message("关闭选课系统成功！");
     }

     @GetMapping("/arrange/info")
     public R queryArrangeCourse(@RequestParam Integer year, @RequestParam String semester) {
          return R.ok().data(openCourseService.getTimeTableIndex(year, semester));

     }

     /**
      * 查询已排课信息
      *
      * @param reqQueryHasArrangeCourse
      * @return
      */
     @PostMapping("/has-arrange/info")
     public R queryHasArrangeCourseInfo(@RequestBody @Validated ReqQueryHasArrangeCourse reqQueryHasArrangeCourse) {

          RespQueryHasArrangeCourse hasArrangeCourseInfos =
                  openCourseService.getHasArrangeCourseInfo(reqQueryHasArrangeCourse);
          return R.ok().data(hasArrangeCourseInfos);
     }

     /**
      * 排课索引
      * @param year 年
      * @param semester 学期
      * @return
      */
     @GetMapping("/timetable/index")
     public R queryTimeTableIndex(@RequestParam Integer year, @RequestParam String semester) {

          RespTimeTableIndex resp = openCourseService.getTimeTableIndex(year, semester);
          return R.ok().data(resp);

     }

     /**
      * 排课索引
      * @param year 年
      * @param semester 学期
      * @return
      */
     @GetMapping("/timetable/delete")
     public R deleteTimeTable(@RequestParam Integer year, @RequestParam String semester) {

          openCourseService.deleteTimeTable(year, semester);
          return R.ok();

     }



}
